﻿using AnlianCommon.Common.Domains.BreedingManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace SheepCommon.API.SheepCommands.SheepWeighedes
{
    public class SheepWeighedCreateHandler : IRequestHandler<SheepWeighedCreateCommand, Result>
    {
        private readonly IBaseRepository<SheepWeighed> _sheepWeighedRepository;
        private readonly IMapper _mapper;

        public SheepWeighedCreateHandler(IBaseRepository<SheepWeighed> sheepWeighedRepository, IMapper mapper)
        {
            _sheepWeighedRepository = sheepWeighedRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepWeighedCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code=(int)CommonErrorCode.Added};
            try
            {
                SheepWeighed sheepWeighed = _mapper.Map<SheepWeighed>(request);
                sheepWeighed.CreatedOwnerId = "张三";
                sheepWeighed.CreatedTime = DateTime.Now;
                var res = await _sheepWeighedRepository.Add(sheepWeighed);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Code = (int)CommonErrorCode.Added;
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
